# -*- coding: utf-8 -*-

# __date:       2021/6/10
# __author:     Yang Chao
# __function:   Add Two Numbers

# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:

    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        # 76ms  14.9MB
        dunmmy = ListNode(0)
        t = dunmmy
        t1 = l1
        t2 = l2
        carry = 0
        while t1 or t2:
            num1 = t1.val if t1 else 0
            num2 = t2.val if t2 else 0
            t1 = t1.next if t1 else t1
            t2 = t2.next if t2 else t2
            cur = (num1 + num2 + carry) % 10
            carry = (num1 + num2 + carry) // 10
            t.next = ListNode(cur)
            t = t.next
        if carry:
            t.next = ListNode(carry)
        return dunmmy.next

